import type { Recordable, UserInfo } from "@general/types";

import { useAccessStore, useUserStore } from "@general/stores";
import { defineStore } from "pinia";
import { ref } from "vue";
import { useRouter } from "vue-router";
// import { getAccessCodesApi, getUserInfoApi, loginApi, logoutApi } from "#/api";

export const useAuthStore = defineStore("auth", () => {
  const accessStore = useAccessStore();
  const userInfo = useUserStore();
  const router = useRouter();
  const loginLoading = ref(false);

  /**
   * 异步处理登录操作
   * Asynchronously handle the login process
   * @param params 登录表单数据
   */
  async function authLogin(
    params: Recordable<any>,
    onSuccess?: () => Promise<void> | void
  ) {
    // 异步处理用户登录操作并获取 accessToken
    let userInfo: null | UserInfo = null;
    try {
      loginLoading.value = true;
      // TODO： 登录API
      const accessToken = null;

      // const { accessToken } = await loginApi(params);
      if (accessToken) {
        accessStore.setAccessToken(accessToken);
      }
    } catch (e) {}
  }
});
